source("functions.R")
source("models.R")

events <- yaml::read_yaml("events.yml")

local({
    dat <- cbind(readRDS("data_lss_ja.RDS"), 
                 readRDS("data_newsmap_ja.RDS"), 
                 readRDS("data_dictionary_ja.RDS"))
    dat <- subset(dat, class %in% c("kp", "jp") & date <= as.Date("2018-12-31"))
    result <- data.frame()
    for (w in seq(10, 90, 1)) {
        cat(w, "\n")
        temp <- flag_events(dat, events$JP, w, merge = FALSE)
        summ <- summary(lm(f5, temp))
        result <- rbind(result, data.frame(
            window = w,
            r2 = summ$r.squared)
        )
    }
    saveRDS(result, "data_optimize_ja.RDS")
})

local({
    dat <- cbind(readRDS("data_lss_he.RDS"), 
                 readRDS("data_newsmap_he.RDS"), 
                 readRDS("data_dictionary_he.RDS"))
    dat <- subset(dat, class %in% c("ir", "il") & date <= as.Date("2018-12-31"))
    result <- data.frame()
    for (w in seq(10, 90, 1)) {
        cat(w, "\n")
        temp <- flag_events(dat, events$IL, w, merge = FALSE)
        summ <- summary(lm(f5, temp))
        result <- rbind(result, data.frame(
            window = w,
            r2 = summ$r.squared)
        )
    }
    saveRDS(result, "data_optimize_he.RDS")
})
